package 剑指offer;

import java.util.HashMap;
import java.util.Map;

/**
 * 最长不含重复字符的子字符串
 */
public class Offer48_JZ {
    public static int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> window = new HashMap<>();
        int left = 0;
        int right = 0;
        int len = Integer.MIN_VALUE;
        while (right < s.length()) {
            char c1 = s.charAt(right);
            right++;
            window.put(c1, window.getOrDefault(c1, 0) + 1);
            while(window.get(c1) > 1){
                char c2 = s.charAt(left);
                left++;
                window.put(c2, window.get(c2)-1);
                if (window.get(c2) == 0) {
                    window.remove(c2);
                }
            }
            if (window.size() > len) {
                len = window.size();
            }
        }
        return len==Integer.MIN_VALUE ? 0 : len;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }
}
